#include <QtGui>

#include "mainwindow.h"
#include "filemanager.h"
#include "element.h"
#include "scene.h"

MainWindow::MainWindow()
{
    scene = new Scene();
    connect(scene, SIGNAL(itemSelected(Element*)), this, SLOT(itemSelected(Element*)));
    connect(scene, SIGNAL(circleMoveInsert(Element*)), this, SLOT(circleMoveInserted(Element*)));

    fileManager = new FileManager(scene);

    createActions();
    createToolBar();
    createCircleMoveSettings();
    createPathMoveSettings();
    setStatusBar(statusBar());

    fileManager->setSpinner(spinner);

    QHBoxLayout *layout = new QHBoxLayout();

    view = new QGraphicsView(scene);
    view->setMaximumSize(scene->sceneRect().width() + SCENE_BORDER, scene->sceneRect().height() + SCENE_BORDER);
    layout->addWidget(view);

    QWidget *widget = new QWidget;
    widget->setLayout(layout);

    setCentralWidget(widget);
    setWindowTitle(tr("Level editor"));
}

void MainWindow::itemSelected(Element *element)
{
    buttonGroup->button(element->moveType)->setChecked(true);
}

void MainWindow::createActions()
{
    saveAction = new QAction(QIcon(":/images/save.png"), tr("&Save level"), this);
    saveAction->setShortcut(tr("Ctrl+S"));
    saveAction->setStatusTip(tr("Save level"));
    connect(saveAction, SIGNAL(triggered()), fileManager, SLOT(save()));

    loadAction = new QAction(QIcon(":/images/load.png"), tr("&Load level"), this);
    loadAction->setShortcut(tr("Ctrl+L"));
    loadAction->setStatusTip(tr("Load level"));
    connect(loadAction, SIGNAL(triggered()), fileManager, SLOT(load()));

    deleteAction = new QAction(QIcon(":/images/delete.png"), tr("&Delete element"), this);
    deleteAction->setShortcut(tr("Delete"));
    deleteAction->setStatusTip(tr("Delete element"));
    connect(deleteAction, SIGNAL(triggered()), scene, SLOT(deleteItem()));

    QActionGroup *actionGroup = new QActionGroup(this);

    squareAction = new QAction(QIcon(":/images/square.png"), tr("&Pick square"), actionGroup);
    squareAction->setShortcut(tr("S"));
    squareAction->setStatusTip(tr("Pick square"));
    squareAction->setCheckable(true);
    squareAction->setChecked(true);
    connect(squareAction, SIGNAL(triggered()), scene, SLOT(pickSquare()));

    circleAction = new QAction(QIcon(":/images/circle.png"), tr("&Pick circle"), actionGroup);
    circleAction->setShortcut(tr("C"));
    circleAction->setStatusTip(tr("Pick circle"));
    circleAction->setCheckable(true);
    connect(circleAction, SIGNAL(triggered()), scene, SLOT(pickCircle()));
}

void MainWindow::createToolBar()
{
    QToolBar *toolBar;
    toolBar = addToolBar(tr("Actions"));
    toolBar->addAction(saveAction);
    toolBar->addAction(loadAction);
    toolBar->addSeparator();
    toolBar->addAction(deleteAction);
    toolBar->addSeparator();
    toolBar->addAction(squareAction);
    toolBar->addAction(circleAction);

    toolBar = addToolBar(tr("Moves"));
    addToolBarBreak();
    addToolBar(toolBar);

    buttonGroup = new QButtonGroup(this);
    connect(buttonGroup, SIGNAL(buttonClicked(int)), this, SLOT(moveTypeSelected(int)));
    
    QRadioButton *button;
    button = new QRadioButton("&NoMove");
    button->setStatusTip(tr("Element will not move"));
    button->setChecked(true);
    buttonGroup->addButton(button, NoMove);
    toolBar->addWidget(button);

    button = new QRadioButton("C&ircleMove");
    button->setStatusTip(tr("Element will move along a circular trajectory"));
    buttonGroup->addButton(button, CircleMove);
    toolBar->addWidget(button);
    connect(button, SIGNAL(toggled(bool)), this, SLOT(showCircleMoveSettings(bool)));

    button = new QRadioButton("&FreeMove");
    button->setStatusTip(tr("Element will move freely and bounce against the walls"));
    buttonGroup->addButton(button, FreeMove);
    toolBar->addWidget(button);

    button = new QRadioButton("&PathMove");
    button->setStatusTip(tr("Element will move along given path"));
    buttonGroup->addButton(button, PathMove);
    toolBar->addWidget(button);
    connect(button, SIGNAL(toggled(bool)), this, SLOT(showPathMoveSettings(bool)));
}

void MainWindow::createCircleMoveSettings()
{
    circleMoveSettings = new QDialog(this);
    circleMoveSettings->setGeometry(0, 0, 150, 50);
    circleMoveSettings->setWindowTitle("Radius");

    spinner = new QSpinBox();
    spinner->setMaximum(SCENE_HEIGHT * 2);
    spinner->setValue(80);
    connect(spinner, SIGNAL(valueChanged(int)), this, SLOT(circleRadiusChanged(int)));

    QHBoxLayout *layout = new QHBoxLayout();
    layout->addWidget(spinner);

    circleMoveSettings->setLayout(layout);
}

void MainWindow::createPathMoveSettings()
{
    pathMoveSettings = new QDialog(this);
    pathMoveSettings->setGeometry(0, 0, 150, 50);
    pathMoveSettings->setWindowTitle("Path");

    QPushButton *button = new QPushButton("Adding nodes");
    button->setCheckable(true);
    button->setFocusPolicy(Qt::NoFocus);
    connect(button, SIGNAL(toggled(bool)), this, SLOT(nodeToggled(bool)));

    QHBoxLayout *layout = new QHBoxLayout();
    layout->addWidget(button);

    pathMoveSettings->setLayout(layout);
}

void MainWindow::nodeToggled(bool toggled)
{
    if (toggled)
    {
        scene->elementType = Scene::NodeType;
    }
    else
    {
        if (squareAction->isChecked())
        {
            scene->pickSquare();
        }
        else
        {
            scene->pickCircle();
        }
    }
}

void MainWindow::moveTypeSelected(int typeId)
{
    scene->moveType = MoveType(typeId);
    foreach (QGraphicsItem *item, scene->selectedItems())
    {
        Element *element = (Element*) item;
        element->moveType = scene->moveType;

        if (scene->moveType == CircleMove)
        {
            element->setMoveCircleRadius(spinner->value());
        }
        else
        {
            element->setMoveCircleRadius(0);
        }
    }
}

void MainWindow::circleMoveInserted(Element *element)
{
    element->setMoveCircleRadius(spinner->value());
}

void MainWindow::showCircleMoveSettings(bool visible)
{
    circleMoveSettings->setVisible(visible);
}

void MainWindow::showPathMoveSettings(bool visible)
{
    pathMoveSettings->setVisible(visible);
}

void MainWindow::circleRadiusChanged(int radius)
{
    foreach (QGraphicsItem *item, scene->selectedItems())
    {
        Element *element = (Element*) item;
        element->setMoveCircleRadius(radius);
    }
}
